import { Component, OnInit } from '@angular/core'
import { Location } from '@angular/common'
import { ActivatedRoute, Params } from '@angular/router'
import { EstateAppointService } from './services/estate-appoint.service'
import { LoginService } from 'src/app/common/services/login.service'
import { CoolLocalStorage } from 'src/app/common/coolStorage/cool-local-storage'
import { CommonUtil } from 'src/app/common/utils/common.util'

@Component({
  selector: 'app-estate-appoint',
  templateUrl: './estate-appoint.component.html',
  styleUrls: ['./estate-appoint.component.scss']
})
export class EstateAppointComponent implements OnInit {
  mode: string
  id: string
  worker: any

  name: string = ''
  phone: string = ''
  captcha: string
  activeSex: string = '先生'
  zone = ''
  remark = ''

  disabled = false
  needCaptcha = false
  sending = false
  leftTime = 60
  constructor(
    private location: Location,
    private activatedRoute: ActivatedRoute,
    private service: EstateAppointService,
    private loginService: LoginService,
    private storage: CoolLocalStorage,
    private utils: CommonUtil
  ) {
    activatedRoute.params.subscribe((params: Params) => {
      this.mode = params.mode
      this.id = params.id
    })
  }

  ngOnInit() {
    this.zone = this.storage.getItem('zone')
    this.worker = this.storage.getObject('worker')
  }

  onClickLeft() {
    this.location.back()
  }
  selectSex(index) {
    this.activeSex = index
  }
  sendCaptchaAction() {
    let phoneReg = /^1\d{10}$/
    if (!this.phone) {
      this.utils.info('请输入您的手机号码')
    } else if (!phoneReg.test(this.phone)) {
      this.utils.info('手机号码格式错误，请重新输入')
    } else {
      this.sendCaptchaReq()
    }
  }
  sendCaptchaReq() {
    let msg = '验证码发送失败，请重试'
    let reqdata = { mode: 'CaptchaMode_Mobile', target: this.phone }
    this.utils.loading('正在发送验证码')
    this.sending = true
    this.loginService.getCaptcha(reqdata, res => {
      if (res.Success && res.Result) {
        this.utils.success('验证码已发送')
        this.countDown()
      } else {
        if (res.Message && res.Message.indexOf('|') > -1) {
          msg = res.Message.split('|')[0]
        }
        this.utils.info(msg)
      }
    })
  }
  submitAction() {
    let phoneReg = /^1\d{10}$/
    if (!this.name) {
      this.utils.info('请输入您的称呼')
    } else if (!this.phone) {
      this.utils.info('请输入您的手机号码')
    } else if (!phoneReg.test(this.phone)) {
      this.utils.info('手机号码格式错误，请重新输入')
    } else if (!this.captcha) {
      this.utils.info('请输入验证码')
    } else {
      this.submitReq()
    }
  }
  submitReq() {
    this.utils.loading('正在预约中...')
    this.disabled = true
    let reqdata = {
      captcha: this.captcha || null,
      linkman: this.name + ' ' + this.activeSex,
      phone: this.phone,
      project: this.id,
      zone: this.zone,
      remark: this.remark
    }
    if (this.worker) {
      reqdata['worker'] = this.worker.Id
    }
    this.service.submit(
      reqdata,
      res => {
        this.utils.hide()
        this.disabled = false
        let msg = '预约失败，请重试'
        if (res.Success && res.Result) {
          this.utils.success('恭喜您，预约成功')
          this.location.back()
        } else {
          if (res.Message && res.Message.indexOf('|') > -1) {
            msg = res.Message.split('|')[0]
          }
          this.utils.info(msg)
        }
      },
      () => {
        this.utils.hide()
        this.disabled = false
      }
    )
  }
  countDown() {
    let leftTime = this.leftTime
    let timer = setInterval(() => {
      if (leftTime === 1) {
        clearInterval(timer)
        this.leftTime = 60
      } else {
        leftTime--
        this.leftTime = leftTime
      }
    }, 1000)
  }
}
